package com.example.xy_assistant.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.xy_assistant.R;
import com.example.xy_assistant.bean.Note;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class NoteAdapter extends ArrayAdapter<Note> {

    SimpleDateFormat format;
    Context context;
    List<Note> notes;

    @SuppressLint("SimpleDateFormat")
    public NoteAdapter(@NonNull Context context, List<Note> notes) {
        super(context, R.layout.litem_note, notes);
        this.context = context;
        this.notes = notes;
        format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.CHINA);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.litem_note, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_1.setText(notes.get(position).getTitle());
        long time = notes.get(position).getTime();
        Date date = new Date(time);
        viewHolder.tv_2.setText(this.format.format(date));
        return convertView;
    }

    public static
    class ViewHolder {
        public View rootView;
        public TextView tv_1;
        public TextView tv_2;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_1 = (TextView) rootView.findViewById(R.id.tv_1);
            this.tv_2 = (TextView) rootView.findViewById(R.id.tv_2);
        }

    }
}
